const CommomUtil = require('../utils/CommomUtil');
const SessionUtil = require('../utils/SessionUtil');
const redis_session = require('../config').redis_user;
const albums_dao = require('../dao/albums_dao');
const user_dao = require('../dao/user_dao');

/** 创建音乐专辑 或 歌单 */
module.exports.c_albums = create_albums_info;
async function create_albums_info(ctx) {

    let { ty_id, ty_s_id, cover_id, albums_name, albums_desc, albums_tags, type } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(albums_tags, 100) || CommomUtil.isEmpty(cover_id)
        || CommomUtil.inValid(albums_name, 30) || CommomUtil.isEmpty(albums_desc, 2000) || CommomUtil.isEmpty(type)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(cover_id) || !CommomUtil.isNumber(type) || type < 0 || type > 1) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let albums_id = await albums_dao.create_albums_info(cover_id, albums_name, albums_desc, ty_id, albums_tags, type);
    if (albums_id < 0) {
        resData['ret'] = false;
        resData['msg'] = '创建失败!';
        return ctx.body = resData;
    }
    resData['albums_id'] = albums_id;
    resData['albums_name'] = albums_name;
    resData['ret'] = true;
    resData['msg'] = '创建成功!';
    return ctx.body = resData;
}

/** 获取我创建的专辑列表 */
module.exports.qml_albums = query_my_albums_list;
async function query_my_albums_list(ctx) {
    let { ty_id, ty_s_id, begin, plen } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.isEmpty(begin) || CommomUtil.isEmpty(plen)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(begin) || !CommomUtil.isNumber(plen) || begin < 0 || plen < 1) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let [total, albums_list] = await albums_dao.query_my_albums_list(begin, plen, ty_id, 0);
    resData['albums_list'] = albums_list;
    resData['total'] = total;
    resData['ret'] = true;
    resData['msg'] = '查询成功!';
    return ctx.body = resData;
}

/** 创建歌单 */
module.exports.c_playlist = create_play_list;
async function create_play_list(ctx) {

    let { ty_id, ty_s_id, playlist_name } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(playlist_name, 30)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let albums_id = await albums_dao.create_playlist(playlist_name, ty_id);
    if (albums_id < 0) {
        resData['ret'] = false;
        resData['msg'] = '创建失败!';
        return ctx.body = resData;
    }
    resData['albums_id'] = albums_id;
    resData['ret'] = true;
    resData['msg'] = '创建成功!';
    return ctx.body = resData;
}

/** 删除我创建的音乐歌单 */
module.exports.del_my_play_list = del_my_play_list;
async function del_my_play_list(ctx) {
    let { ty_id, ty_s_id, playlist_id } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(playlist_id, 32, 32)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let [flag, msg] = await albums_dao.del_playlist(playlist_id, ty_id);
    resData['ret'] = flag;
    resData['msg'] = msg;
    return ctx.body = resData;
}

/** 更新歌单或专辑信息 */
module.exports.update_my_albums_info = update_my_albums_info;
async function update_my_albums_info(ctx) {
    let { ty_id, ty_s_id, item_id, title, tags, intor, cover_id } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(item_id, 32) || CommomUtil.inValid(title, 32)
        || CommomUtil.inValid(tags, 100) || CommomUtil.inValid(intor, 1000) || CommomUtil.isEmpty(cover_id)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(cover_id)) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let flag = await albums_dao.update_my_albums_info(item_id, title, intor, tags, cover_id, ty_id);
    if (flag) {
        resData['ret'] = false;
        resData['msg'] = '更新失败!';
        return ctx.body = resData;
    }
    resData['ret'] = true;
    resData['msg'] = '更新成功!';
    return ctx.body = resData;
}

/** 添加音乐到歌单 */
module.exports.add_music_to_playlist = add_music_to_playlist;
async function add_music_to_playlist(ctx) {

    let { ty_id, ty_s_id, playlist_id, music_id } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(playlist_id, 32) || CommomUtil.inValid(music_id, 32)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let [flag, msg] = await albums_dao.add_music_to_playlist(playlist_id, music_id, ty_id);
    resData['ret'] = flag;
    resData['msg'] = msg;
    return ctx.body = resData;
}

/** 添加音乐列表到歌单 */
module.exports.add_music_list_to_playlist = add_music_list_to_playlist;
async function add_music_list_to_playlist(ctx) {

    let { ty_id, ty_s_id, playlist_id, music_list_id } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(playlist_id, 32) || CommomUtil.isEmpty(music_list_id)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    // if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
    //     resData['ret'] = false;
    //     resData['msg'] = '请先登录!';
    //     return ctx.body = resData;
    // }
    let [flag, msg] = await albums_dao.add_music_list_to_playlist(playlist_id, music_list_id, ty_id);
    resData['ret'] = flag;
    resData['msg'] = msg;
    return ctx.body = resData;
}

/** 添加 专辑或歌单到收藏 */
module.exports.add_albums_to_favor = add_albums_to_favor;
async function add_albums_to_favor(ctx) {
    let { ty_id, ty_s_id, albums_id } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(albums_id, 32)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let [flag, msg, favor_status] = await albums_dao.add_albums_to_favor(albums_id, ty_id);
    resData['favor_status'] = favor_status;
    resData['ret'] = flag;
    resData['msg'] = msg;
    return ctx.body = resData;
}


/** 获取我创建的歌单列表 */
module.exports.qlm_playlist = ql_my_playlist;
async function ql_my_playlist(ctx) {
    let { ty_id, ty_s_id, begin, plen } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(begin) || CommomUtil.isEmpty(plen)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(begin) || !CommomUtil.isNumber(plen) || begin < 0 || plen < 1) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }
    // || CommomUtil.isEmpty(ty_s_id)
    // if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
    //     resData['ret'] = false;
    //     resData['msg'] = '请先登录!';
    //     return ctx.body = resData;
    // }
    let [total, albums_list] = await albums_dao.query_my_albums_list(begin, plen, ty_id, 1);
    resData['play_lists'] = albums_list;
    resData['total'] = total;
    resData['ret'] = true;
    resData['msg'] = '查询成功!';
    return ctx.body = resData;
}

/** 我的专辑音乐播放列表 */
module.exports.query_albums_play_list = query_albums_play_list;
async function query_albums_play_list(ctx) {
    let { ty_id, ty_s_id, albums_id, type, begin, plen } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(albums_id, 32)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    /** 为了兼容旧接口 赋默认值 即查询的是专辑 */
    if (CommomUtil.isEmpty(type)) {
        type = 0;
    }
    if (CommomUtil.isEmpty(begin) || CommomUtil.isEmpty(plen) || !CommomUtil.isNumber(begin) || !CommomUtil.isNumber(plen) || !CommomUtil.isNumber(type) || type > 1 || type < 0) {
        begin = 0;
        plen = 15;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let play_list = await albums_dao.query_albums_play_list(albums_id, type, begin, plen);
    if (play_list.length < 1) {
        resData['play_list'] = [];
        resData['ret'] = false;
        resData['msg'] = '暂无数据!';
        return ctx.body = resData;
    }
    resData['play_list'] = play_list;
    resData['ret'] = true;
    resData['msg'] = '查询成功!';
    return ctx.body = resData;
}

/** 专辑 点赞/取消点赞  */
module.exports.hit_like_albums = hit_like_albums;
async function hit_like_albums(ctx) {
    let { ty_id, ty_s_id, albums_id } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.isEmpty(albums_id)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (CommomUtil.inValid(albums_id, 32)) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }

    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }

    let like_num = await albums_dao.get_albums_like_num(albums_id);
    if (like_num == null) {
        resData['ret'] = false;
        resData['msg'] = '非法操作!';
        return ctx.body = resData;
    }
    let key = ty_id + '_like_' + albums_id;
    let str = await redis_session.get(key);
    let like_status = true;
    if (CommomUtil.isEmpty(str)) {
        if (await redis_session.set(key, 1) == 'OK') {
            let flag = await albums_dao.update_albums_like_num(albums_id, 1);
            if (flag) like_num += 1;
            like_status = true;
        }
    } else {
        if (await redis_session.del(key)) {
            let flag = await albums_dao.update_albums_like_num(albums_id, -1);
            if (flag) like_num -= 1;
            like_status = false;
        }
    }
    resData['like_num'] = like_num;
    resData['like_status'] = like_status;
    resData['ret'] = true;
    resData['msg'] = '操作成功!';
    return ctx.body = resData;
}


/** 我的音乐：我收藏的歌单 、 专辑 */
module.exports.my_favor_list = my_favor_list;
async function my_favor_list(ctx) {
    let { ty_id, ty_s_id, begin, plen, type } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(begin) || CommomUtil.isEmpty(plen) || CommomUtil.isEmpty(type)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(begin) || !CommomUtil.isNumber(plen) || begin < 0 || plen < 1 || !CommomUtil.isNumber(type) || type < 0 || type > 1) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }
    // if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
    //     resData['ret'] = false;
    //     resData['msg'] = '请先登录!';
    //     return ctx.body = resData;
    // }
    let [total, favor_list] = await albums_dao.query_my_favor_playlist_or_albums(begin, plen, ty_id, type);
    resData['favor_list'] = favor_list;
    resData['total'] = total;
    resData['ret'] = true;
    resData['msg'] = '获取成功!';
    return ctx.body = resData;
}

/** 获取歌单详细信息 */
module.exports.query_albums_info = query_albums_info;
async function query_albums_info(ctx) {
    let { ty_id, ty_s_id, albums_id } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(albums_id, 32)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }

    let [flag, data] = await albums_dao.query_albums_info(albums_id, ty_id);
    let key = ty_id + '_like_' + albums_id;
    let like_status = false;

    if (flag) {
        let str = await redis_session.get(key);
        if (!CommomUtil.isEmpty(str)) like_status = true;
    }
    flag ? (resData = data, resData['msg'] = '查询成功!', resData['like_status'] = like_status) : resData['msg'] = data;
    resData['ret'] = flag;
    return ctx.body = resData;
}

/** 查询歌单或专辑详情 */
module.exports.query_albums_info2 = query_albums_info2;
async function query_albums_info2(ctx) {
    let { ty_id, ty_s_id, albums_id } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.inValid(albums_id, 32)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    let [flag, data] = await albums_dao.query_albums_info2(albums_id);

    flag ? (resData = data, resData['msg'] = '查询成功!') : resData['msg'] = data;
    resData['ret'] = flag;
    return ctx.body = resData;
}


/** 分类歌单 */
module.exports.query_category_play_list = query_category_play_list;
async function query_category_play_list(ctx) {
    let { ty_id, ty_s_id, begin, plen, tag } = CommomUtil.getReqData(ctx);
    let resData = {};
    if (CommomUtil.inValid(ty_id, 32) || CommomUtil.isEmpty(ty_s_id) || CommomUtil.isEmpty(begin) || CommomUtil.isEmpty(plen)) {
        resData['ret'] = false;
        resData['msg'] = '参数不全!';
        return ctx.body = resData;
    }
    if (!CommomUtil.isNumber(begin) || !CommomUtil.isNumber(plen) || begin < 0 || plen < 1) {
        resData['ret'] = false;
        resData['msg'] = '参数错误!';
        return ctx.body = resData;
    }
    if (!(await SessionUtil.checkTySession(ty_id, ty_s_id))) {
        resData['ret'] = false;
        resData['msg'] = '请先登录!';
        return ctx.body = resData;
    }
    if (CommomUtil.isEmpty(tag)) {
        tag = null;
    }
    let albums_list = await albums_dao.query_category_albums_list(begin, plen, tag);
    /** 异步循环 */
    let play_list = [];
    for (let albums of albums_list) {
        /** 设置用户信息 */
        play_list.push(user_dao.set_albums_user_name_and_img(albums));
    }
    let data = [];
    await Promise.all(play_list).then((resultLists) => {
        for (let item of resultLists) {
            data.push(item);
        }
    });
    resData['play_list'] = data;
    resData['ret'] = true;
    resData['msg'] = '查询成功';
    return ctx.body = resData;
}